//
//  StatusOriginalView.swift
//  3月5的项目
//
//  Created by 哦哦 on 16/3/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


//定义全局的间距 Original 原来的
let StatusCellMargin: CGFloat = 10
class StatusOriginalView: UIView {
    //约束属性 约束对象类型 是
    var bottomConstraint : Constraint?
    
    var status: Status? {
        didSet {
            //绑定模型数据
            iconView.sd_setImageWithURL(status?.user?.headImageURL)
            nameLabel.text = status?.user?.name
            mbrank_image.image = status?.user?.mbrank_image
            verified_image.image = status?.user?.verified_type_image
            //TODO: 后期完善  OC中可以使用警告来标示待做事项
            //            sourceLabel.text = status?.source
            //            timeLabel.text = status?.created_at
            contentLabel.text = status?.text
            
            //给配图视图数据源赋值
            //            pictureView.imageURLs = status?.imageURLs
            
            //原创微博没有配图视图的情况下 数组不为空
            print(status?.imageURLs?.count)
            //在动态更新约束之前 卸载约束 在cell复用的情况下 约束也会被复用
            bottomConstraint?.uninstall()
            //根据是否有配图视图 动态的调整约束
            if let urls = status?.imageURLs where urls.count != 0 {
//                print("原创微博有配图视图")
//                pictureView.imageURLs = status?.imageURLs
                //显示配图视图
                pictureView.hidden = false
                pictureView.imageURLs = urls
                //1.修改原创微博的底部约束: 相对于配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //constraint: 表示获取约束对象
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
//                print("原创微博没有++++++++配图视图")
                //隐藏配图视图
                pictureView.hidden = true

                pictureView.imageURLs = nil
                //相对于 微博正文
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            
            
        }
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: 设置UI
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        //添加子视图
        sepView.backgroundColor = UIColor.redColor()
        addSubview(sepView)
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbrank_image)
        addSubview(verified_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            //距离左边
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            //            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            
        }
        
        verified_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
            
            //搞定数的文本  make.right.equalTo(iconView.snp_right).offset(-StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
            
        }
        //不能够满足的变换
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            //这条约束不需要添加 使用宽度来解决
            //            make.right.equalTo(contentLabel.snp_right)
            
            //设置测试大小
            //            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        
        //将用户头像处理成圆角
        iconView.layer.cornerRadius = 18
        iconView.layer.masksToBounds = true
        
        
        //添加原创微博底部的约束
        self.snp_makeConstraints { (make) -> Void in
            //            make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            //            make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            //保存底部约束对象 并且设置底部约束
            self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    private lazy var sepView: UIView = UIView()
    
    //头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //皇冠
    private lazy var mbrank_image: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //vip
    private lazy var verified_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //名称
    private lazy var nameLabel: UILabel = UIFactory.labelFactory("25期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    //时间
    private lazy var timeLabel: UILabel = UIFactory.labelFactory("25:25", textColor: UIColor.darkGrayColor(), fontSize: 11)
    //来源
    private lazy var sourceLabel: UILabel = UIFactory.labelFactory("隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 11)
    //内容文字
    //    private lazy var contentLabel: UILabel = UIFactory.labelFactory("你若不举,便是晴天", textColor: UIColor.darkGrayColor(), fontSize: 14, alignment: .Left)
    private lazy var contentLabel: UILabel = UIFactory.labelFactory("你若安好,便是晴天", textColor: UIColor.darkGrayColor(), fontSize: 14, alignment: .Left)
    
    //配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
}
